import { mapState, mapMutations } from 'vuex'
import lodash from 'lodash'
import noOrder from "../../components/noOrder/noOrder.vue"
import load from "../../components/load/load.vue"
import netError from "../../components/netError/netError.vue"
import { getNewToken } from '../../config/util'
export default {
	name: 'order',
	data() {
		return {
			paying:false,//正在支付
			netError:false,//网络连接失败
			noOrder:false,//订单为空
			load:true,//加载动画
			activeTab: 'notTravel',
			list: [],
			rows:10,
			page:1,
			busy:true,//是否允许加载更多
			loadingMsg:""
		}
	},
	
	computed:{
		...mapState([
			'token',
		]),	
	},
	methods: {
		...mapMutations([
			'RECORD_ORDERDETAIL','RECORD_TOKEN'
		]),
		handleTabChange(val) {
			this.activeTab = val;
		},
		destination(order){						
			var RT = order.list.some(function(item,index){
				return item.flightSegment==2;
			})
			if(RT){
				for(var i=0;i<order.list.length;i++){
					if(order.list[i].flightSegment==1&&order.list[i].flightSegment!=order.list[i+1].flightSegment){
						return {
					    	'arrCity':order.list[i].arrCity,
							'arrTerminal':order.list[i].arrTerminal
					    };
					}
				}
			}				
			else return {
				 	'arrCity':order.list[order.list.length-1].arrCity,
				 	'arrTerminal':order.list[order.list.length-1].arrTerminal
				};
		},
		toDetail(order) {
			this.RECORD_ORDERDETAIL(order);
			this.$router.push({ path: '/orderDetail' });
		},
		pay(order) {	
			let url = '/pay/pre?temp=' + Math.random();
			let params = { orderNumber : order.orderNumber }
			let status = order.otherStatus;
			if(status=='waiting_for_process'){
				url = '/pay/endorsePre?temp=' + Math.random(); 
				params = { endorseOrderid : order.orderNumber }
			}
			this.$axios.post(url, params,
					{
						headers: {'authorization':this.token}
					}
				)
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});	
					this.payHandle(res);
				})
				
		},
		cancel(order){
	
			this.$axios.get('order/cancel/'+order.orderNumber,
					{
						headers: {'authorization':this.token}
					}
				)
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});	
					if(res.data.success){
						order.orderStatus = 5;
						alert("订单取消成功")						
					}else{
						alert("取消订单失败")
					}
				})
		},
		payHandle(res){
			var status = res.data.status;
			if(status==1001) {
				var url = res.data.resultInfo;
				if(url){
					window.location.href = url;
				}										
			}
			else{
				alert("支付失败")
			}
		},
		loadMore() {
			var that = this;
			this.busy = true;
			setTimeout(function() {
				that.page++
				that.getOrderList(true);
			}, 300)			
		},
		//与netError子组件交互
		netErrorClose(){
			this.netError = false;
		},
		getOrderList(flag) {
			this.$axios.get('/order/query?page='+this.page+'&rows='+this.rows+'&type=phone?temp=' + Math.random(), {
					headers: {'authorization':this.token}					    
				})
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});					
					this.load = false;
					if(res.data.success&&!res.data.success){
						this.netError = true;
						return false;
					}
					if(res.data.total==0){
						this.noOrder = true;
						return false;
					}
					//下拉加载的请求
					if(flag){
						this.list = this.list.concat(res.data.rows);
						if(res.data.total == this.list.length){
							this.busy = true;
							this.loadingMsg = "已经是全部订单";
							return false;
						}
						this.busy = false;
						this.loadingMsg = "努力加载中···";
						return false;
					}
					//初次请求
					this.list = res.data.rows;
					this.busy = false;
				})						
		},		
	},
	components:{
		noOrder,
		load,
		netError,
	},
	created() {
		document.title = "订单管理";
		this.getOrderList();
	}
}